<?php
/**
 * 视图基类
 * @author andery <andery@foxmail.com>
 * @link http://www.minophp.com/
 * @version $Id: CView.php 2010-02-21 00:54:45Z andery $
 */
class CView
{
    // 模板引擎对象
    private $_engine;
    // 单例模式实现
    private static $_instance;
    /**
     * 
	 * 构造函数，初始化视图连接
     */
	public function __construct(){
	    $engineName = CConf::get('tpl.engine');
	    $className   = 'CTemplate'.ucwords($engineName);
	    include_once(NIMO_PATH . '/view/' . $className . '.php');
	    $this->_engine = new $className(CConf::get('tpl.' . $engineName));
	}
	
	public static function getInstance()
	{
	    if (!(self::$_instance instanceof self)) {
	        self::$_instance = new self();
	    }
	    return self::$_instance;
	}
	
	/**
	 * 显示视图
	 * @param string $tplfile 视图名
	 * @param array $tpl_data 传递给视图的变量数组
	 */
	public function display($tplfile, $tpl_data)
	{
		$this->_engine->assign($tpl_data);
		$this->_engine->display($tplfile);
	}
	
	/**
	 * 设置视图变量
	 * 
	 */
	public function assign($tpl_data, $value = '')
    {
        if (!is_array($tpl_data) && $tpl_data != '') {
            $this->_engine->assign($tpl_data, $value);
        } else {
            $this->_engine->assign($tpl_data);
        }
    }
    
    private function __clone(){}
}
